package com.service;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.RoomDAO;
import com.pojo.Room;
import com.util.HardUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class RoomService extends ServiceImpl<RoomDAO, Room> {

    @Autowired
    private RoomDAO roomDAO;


    private RedisTemplate redisTemplate;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${server.url}")
    private String server;

    @Autowired
    private ApplicationContext applicationContext;


    /**
     * 分页查找房间信息
     *
     * @param room
     * @param mac
     * @param online
     * @param page
     * @param size
     * @return
     */
    public IPage search(String room, String mac, Integer online, int page, int size) {
        //分页条件
        Page p = new Page(page, size);
        //查询条件
        QueryWrapper q = new QueryWrapper();
        Map map = new HashMap();
        map.put("room", room);
        map.put("mac", mac);
        map.put("online", online);
        q.allEq(map, false);
        //查询
        IPage result = roomDAO.selectPage(p, q);

        return result;
    }


    /**
     * 心跳上线
     *
     * @param mac
     */
    public void onlineByMac(String mac) {
        QueryWrapper q = new QueryWrapper();
        q.eq("mac", mac);
        Room room = roomDAO.selectOne(q);
        if (room != null) {
            room.setOnline(1);
            roomDAO.updateById(room);
            if (redisTemplate==null)
            {
                redisTemplate = (RedisTemplate)applicationContext.getBean("StringRedisTemplate");
            }
            redisTemplate.boundValueOps("heart_" + mac).set(1, 25, TimeUnit.SECONDS);
        }
    }

    /**
     * 终端下线
     *
     * @param mac
     */
    public void offLine(String mac) {
        QueryWrapper q = new QueryWrapper();
        q.eq("mac", mac);
        Room room = roomDAO.selectOne(q);
        if (room != null) {
            room.setOnline(0);
            roomDAO.updateById(room);
        }
    }

    /**
     * 重启服务器时所有房间号下线
     */
    @PostConstruct
    public void offAllRoom() {
        Room room = new Room();
        room.setOnline(0);
        roomDAO.update(room, null);
    }

    public Room getRoomByMac(String mac) {
        QueryWrapper q = new QueryWrapper();
        q.eq("mac", mac);
        Room room = roomDAO.selectOne(q);
        return room;
    }

    /**
     * 发送新增房间指令
     *
     * @param room
     */
    public void sendSave(Room room) {
        try {
            new Thread(()-> {
                String url = server+"/api/room/save?id={1}";
                String json = JSONObject.toJSONString(room);
                System.out.println(json);
                String id   = HardUtil.getMainBorId();
                restTemplate.postForObject(url,json,String.class,id);
            }
            ).start();
        }catch (Exception e){
             e.printStackTrace();
        }
    }

    /**
     * 发送在线指令
     *
     * @param mac
     * online 1在线 0离线
     */
    public void sendOnline(String mac,int online) {
       try {
           new Thread(()-> {
               String url = server+"/api/room/online?mac={1}&online={2}";
               restTemplate.getForObject(url,String.class,mac,online);
           }
           ).start();
       }catch (Exception e){

       }
    }

    /**
     * 解绑
     * @param mac
     */
    public void sendJieban(String mac)
    {
        try {
            new Thread(()-> {
                String url = server+"/api/room/jieban?mac={1}";
                restTemplate.getForObject(url,String.class,mac);
            }
            ).start();
        }catch (Exception e){

        }
    }

}
